iT邦幫忙

2025 iThome 鐵人賽

DAY 21
0
Software Development

clojure 30 days系列 第 21

clojure 30 days - day 19

  • 分享至 

  • xImage
  •  

Problem Description

Complete the function/method so that it returns the url with anything after the anchor (#) removed.

Examples
"www.codewars.com#about" --> "www.codewars.com"
"www.codewars.com?page=1" -->"www.codewars.com?page=1"

Note

不管使用任何語言,只要處理字串,regex 都是必須會的技能呢。

  • Keywords:
    • clojure.string/split: splits string at # delimiter into vector
    • first: takes the first element from the split result
    • #"#": regex pattern that matches the anchor symbol

Implementation

; implement
(defn remove-url-anchor [url]
  (first (clojure.string/split url #"#")))
  
; Example walkthrough:
; (remove-url-anchor "www.codewars.com#about")
; 1. split: "www.codewars.com#about" -> ["www.codewars.com" "about"]
; 2. first: ["www.codewars.com" "about"] -> "www.codewars.com"


; test
; execute implement function
(defn tester [arg exp]
  (= (remove-url-anchor arg) exp))

; args & exception
(comment
  (tester "www.codewars.com#about" "www.codewars.com")
  (tester "www.codewars.com/katas/?page=1#about" "www.codewars.com/katas/?page=1")
  (tester "www.codewars.com/katas/" "www.codewars.com/katas/"))


上一篇
clojure 30 days - day 18
下一篇
clojure 30 days - day 20
系列文
clojure 30 days25
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言